/**
 * Package:com.goldcitynet.robot;
 * $Id: MainModel.java,v 1.1.1.1 2009/03/23 09:31:14 zhangpei Exp $
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.robot;

import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.afteryuan.util.ConfigHelper;


/**
 * MainModel
 * <p/>
 * <p><a href="MainModel.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: zhangpei $</a>
 * @version $Revision: 1.1.1.1 $
 */
public class MainModel {
    private static Log log = LogFactory.getLog(MainModel.class);
    private int numberStart;
    private int number;
    private int frozenMax;
    private int frozenMin;
    private String sceneId;
    private String sceneName;
    private long total;
    private long totalTime;
    private long averageTime;
    private long maxTime;
    private long minTime;
    private long mostTime;
    private Map<String, String> scenes;
    private MainUI main;

    private static MainModel instance;

    public static synchronized MainModel getInstance(){
        if(instance==null)instance=new MainModel();
        return instance;
    }

    private MainModel() {
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(ConfigHelper.findAsResource("roadmap/scene.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        List<Element> sceneNods = document.selectNodes("//scenes/scene");
        scenes = new HashMap<String, String>();
        for (Element sceneNode : sceneNods) {
            String id = sceneNode.selectSingleNode("id").getText();
            String name = sceneNode.selectSingleNode("name").getText();
            scenes.put(id,name);
        }

    }

    public synchronized void update(long total, long totalTime, long maxTime, long minTime) {
        this.total += total;
        this.totalTime += totalTime;
        this.maxTime=this.maxTime < maxTime?maxTime:this.maxTime;
        this.minTime = this.minTime > minTime?minTime:this.minTime;
        averageTime = this.totalTime/this.total;
        log.debug("update statistic data total:"+this.total+" totalTime:"+this.totalTime+" maxTime:"+this.maxTime+" minTime:"+this.minTime);
        main.updateStatPanel();
    }

    public int getNumberStart() {
        return numberStart;
    }

    public void setNumberStart(int numberStart) {
        this.numberStart = numberStart;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public int getFrozenMax() {
        return frozenMax;
    }

    public void setFrozenMax(int frozenMax) {
        this.frozenMax = frozenMax;
    }

    public int getFrozenMin() {
        return frozenMin;
    }

    public void setFrozenMin(int frozenMin) {
        this.frozenMin = frozenMin;
    }

    public String getSceneId() {
        return sceneId;
    }

    public void setSceneId(String sceneId) {
        this.sceneId = sceneId;
        sceneName = scenes.get(sceneId);
    }

    public String getSceneName() {
        return sceneName;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public long getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
    }

    public long getAverageTime() {
        return averageTime;
    }

    public void setAverageTime(long averageTime) {
        this.averageTime = averageTime;
    }

    public long getMaxTime() {
        return maxTime;
    }

    public void setMaxTime(long maxTime) {
        this.maxTime = maxTime;
    }

    public long getMinTime() {
        return minTime;
    }

    public void setMinTime(long minTime) {
        this.minTime = minTime;
    }

    public long getMostTime() {
        return mostTime;
    }

    public void setMostTime(int mostTime) {
        this.mostTime = mostTime;
    }

    public void setSceneName(String sceneName) {
        this.sceneName = sceneName;
        for(Map.Entry<String,String> entry : scenes.entrySet()){
           if(sceneName.equals(entry.getValue())){
               sceneId=entry.getKey();
               break;
           }
        }


    }

    public Map<String, String> getScenes() {
        return scenes;
    }

    public void setScenes(Map<String, String> scenes) {
        this.scenes = scenes;
    }

    public MainUI getMain() {
        return main;
    }

    public void setMain(MainUI main) {
        this.main = main;
    }
}
